import React, { useEffect, useState } from "react";
import { useTranslation } from "react-i18next";
import { Select, Button, Table, Input, DatePicker } from "antd";
import dayjs from "dayjs";
import customParseFormat from "dayjs/plugin/customParseFormat";
import {
  PlusOutlined,
  EditOutlined,
  DeleteOutlined,
  SearchOutlined,
} from "@ant-design/icons";
import { text_check, post_axios, format_time, add_zero } from "../../utils";

export default function MemberAllinpay(props) {
  dayjs.extend(customParseFormat);
  const { t } = useTranslation();
  const { RangePicker } = DatePicker;
  const [tableHeight, setTableHeight] = useState(0);
  const { actions, setSpinning } = props;
  let { menu_action, user_power_lang, username, user_grade } = actions;
  user_power_lang = JSON.parse(user_power_lang);

  // 国内国外判断
  const session_company = sessionStorage.getItem("company");
  let localStorage_company = JSON.parse(localStorage.getItem("choose_company"));
  const local_company_object =
    localStorage_company &&
    localStorage_company[username] &&
    localStorage_company[username]
      ? localStorage_company[username]
      : "";

  const [log_list, setLog_list] = useState([]);
  const [page, setPage] = useState(1);
  const [rows, setRows] = useState(20);
  const [total, setTotal] = useState(0);

  const getData = async (newPage, newRows) => {
    let upMsg = {
      pageNum: newPage ? newPage : page,
      pageSize: newRows ? newRows : rows,
      status: 0,
      request_type: "findPage",
    };
    setSpinning(true);
    let res = await post_axios("union_pay", "union_pay", upMsg);
    setSpinning(false);
    if (!res) return;
    setLog_list(res.data.BusinessUser);
    setTotal(res.data.Total);
  };
  const columns = [
    {
      dataIndex: "idx",
      align: "center",
      width: 40,
      render: (_, record, index) => <>{index + 1}</>,
    },
    {
      title: "签约户名",
      dataIndex: "signAcctName",
      align: "center",
    },
    {
      title: "提现协议编号",
      dataIndex: "acctProtocolNo",
      align: "center",
    },
    {
      title: "联系人",
      dataIndex: "name",
      align: "center",
    },
    {
      title: "联系电话",
      dataIndex: "phone",
      align: "center",
    },
    {
      title: "会员类型",
      dataIndex: "memberType",
      align: "center",
    },
    {
      title: "提交签订时间",
      dataIndex: "time",
      align: "center",
    },
    {
      title: "签订结果",
      align: "center",
      render: (_, record) => (
        <>
          {record.result == "ok" ? (
            <span>签订成功</span>
          ) : (
            <span>签订失败</span>
          )}
        </>
      ),
    },
  ];
  // 分页器
  const pageOnChange = async (page, pageSize) => {
    setPage(page);
    setRows(pageSize);
    getData(page, pageSize);
  };

  const search_data = () => {
    setPage(1);
    getData(1);
  };
  useEffect(() => {
    // 挂载后或者更新后的操作
    getData().then(() => {
      const tabBoxHeight =
        document.getElementsByClassName("member_allinpay")[0].scrollHeight;
      const tableHeight = tabBoxHeight - 96 - 30;
      setTableHeight(tableHeight);
    });
  }, []);
  return (
    <div className="member_allinpay modules">
      <div className="screen_btn">
        <div className="btn_box">
          {/* 查询 */}
          {menu_action.indexOf("m_query") > -1 ? (
            <Button
              size="small"
              type="text"
              icon={<SearchOutlined style={{ color: "#1677FF" }} />}
              onClick={search_data}
            >
              {text_check(user_power_lang["m_query"])}
            </Button>
          ) : (
            ""
          )}
        </div>
      </div>
      <div className="table_box">
        <div className="page-table">
          <Table
            dataSource={log_list}
            size="small"
            bordered
            columns={columns}
            rowKey="id"
            virtual
            scroll={{
              y: tableHeight,
            }}
            pagination={{
              defaultCurrent: page,
              defaultPageSize: rows,
              size: "small",
              current: page,
              total: total,
              responsive: true,
              showTotal: (total) =>
                text_check({
                  msg_cn: `共 ${total} 条`,
                  msg_en: `Total ${total} items`,
                }),
              onChange: pageOnChange,
            }}
          />
        </div>
      </div>
    </div>
  );
}
